﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections;
using RebEngine.Interfaces;
using RebEngine.Textures;
using System.Threading;

namespace RebEngine.Managers
{
    public class TextureManager : GameComponent
    {
        private static List<string> sentInstructions = new List<string>();
        private static Dictionary<string, IRebTexture> _textures = new Dictionary<string, IRebTexture>();

        private static bool _initialized = false;
        /// <summary>
        /// Is the TextureManagers Initialized, used for test cases and setup of Effects.
        /// </summary>
        public static bool Initialized
        {
            get { return _initialized; }
        }

        private static int _texturesLoaded = 0;
        /// <summary>
        /// The number of textures that are currently loaded.
        /// Use this for user loading bar feedback.
        /// </summary>
        public static int TexturesLoaded
        {
            get { return _texturesLoaded; }
        }

        /// <summary>
        /// Create the shader Managers.
        /// </summary>
        /// <param name="game"></param>
        public TextureManager(Game game)
            : base(game)
        {
            AddTexture("placeHolder");
        }

        /// <summary>
        /// Add a shader of type RoeTexture.
        /// </summary>
        /// <param name="newTexture"></param>
        /// <param name="textureName"></param>
        public static void AddTexture(string textureName)
        {
            RebTexture newTexture = new RebTexture(textureName);

            if (textureName != null && !_textures.ContainsKey(textureName))
            {
                _textures.Add(textureName, newTexture);

                if (_initialized)
                {
                        newTexture.LoadContent();
                        _texturesLoaded++;
                }
            }
        }

        public static void AddTexture(string type, string textureName)
        {
            RebTexture newTexture = new RebTexture(type +"/"+ textureName);

            if (textureName != null && !_textures.ContainsKey(textureName))
            {
                _textures.Add(textureName, newTexture);

                if (_initialized)
                {
                    ThreadStart threadStarter = delegate
                    {
                        newTexture.LoadContent();
                        _texturesLoaded++;
                    };
                    Thread loadingThread = new Thread(threadStarter);
                    loadingThread.Start();
                }
            }
        }


        /// <summary>
        /// Remove a texture from memory
        /// </summary>
        /// <param name="textureName"></param>
        public static void RemoveTexture(string textureName)
        {
            if (textureName != null && _textures.ContainsKey(textureName))
            {
                if (_initialized)
                {
                    ThreadStart threadStarter = delegate
                    {
                        _textures[textureName].UnloadContent();
                        _textures.Remove(textureName);
                        _texturesLoaded--;
                    };
                    Thread loadingThread = new Thread(threadStarter);
                    loadingThread.Start();
                }
            }
        }

        /// <summary>
        /// Get a texture
        /// </summary>
        /// <param name="textureId"></param>
        /// <returns></returns>
        public static IRebTexture GetTexture(string textureName)
        {
            if (textureName != null && _textures.ContainsKey(textureName) && _textures[textureName].BaseTexture != null)
            {
                return _textures[textureName];
            }
            else
            {
                string message = "TextureManager: Missing Texture Content named " + textureName + ". Loading default";
                if (!sentInstructions.Contains(message))
                {
                    sentInstructions.Add(message);
                    LogManager.Write(message);
                }
                return _textures["placeHolder"];
            }
        }

        public override void Initialize()
        {
            base.Initialize();

            ThreadStart threadStarter = delegate
            {
                foreach (IRebTexture texture in _textures.Values)
                {
                    if (!texture.ReadyToRender)
                    {
                        texture.LoadContent();
                        _texturesLoaded++;
                    }
                }
            };
            Thread loadingThread = new Thread(threadStarter);
            loadingThread.Start();

            _initialized = true;
        }
    }
}